package com.storm.lib.util;

import java.util.ArrayList;
import java.util.List;

import com.storm.lib.vo.IdNumberVo;


public class DBFieldUtil {
	public static String emptySignore=" ";
	
	public static List<Integer> getIntegerListByCommer(String str){
		if(str==null){
			return new ArrayList<Integer>();
		}
		String[] strs = str.split(",");
		if(strs.length==0){
			return new ArrayList<Integer>();
		}
		else{
			List<Integer> intList = new ArrayList<Integer>();
			for (int i = 0; i < strs.length; i++) {
				intList.add(Integer.valueOf(strs[i]));
			}
			return intList;
		}
	}
	
	
	public static List<Long> getLongListByCommer(String str){
		if(str==null){
			return new ArrayList<Long>();
		}
		String[] strs = str.split(",");
		if(strs.length==0){
			return new ArrayList<Long>();
		}
		else{
			List<Long> intList = new ArrayList<Long>();
			for (int i = 0; i < strs.length; i++) {
				intList.add(Long.valueOf(strs[i]));
			}
			return intList;
		}
	}
	
	public static String implodInterListWithCommer(List<Integer> list){
		if(list==null || list.size()==0){
			return null;
		}
		String[] strs = new String[list.size()];
		for (int i=0;i<strs.length;i++) {
			strs[i]=list.get(i).toString();
		}
		return StringUtil.implode(strs, ",");
	}
	/**
	 * 
	 * 方法功能:建立连接属性的String
	 * 更新时间:2011-10-28, 作者:johnny
	 * @param objs
	 * @param declim
	 * @return
	 */
	public static String createPropertyImplod(Object[] objs,String declim){
		StringBuilder sb = new StringBuilder();
		if(objs.length==1){
			return fetchDBFieldValue(objs[0]);
		}
		for (int i = 0; i < objs.length-1; i++) {
			sb.append(fetchDBFieldValue(objs[i]));
			sb.append(declim);
		}
		sb.append(fetchDBFieldValue(objs[objs.length-1]));
		return sb.toString();
		
	}

	public static String fetchDBFieldValue(Object object) {
		if(object==null){
			return emptySignore;
		}
		if(object instanceof String){
			return ((String)object);
		}
		if(object instanceof Integer){
			return ((Integer)object).toString();
		}
		if(object instanceof Short){
			return ((Short)object).toString();
		}
		if(object instanceof Long){
			return ((Long)object).toString();
		}
		ExceptionUtil.throwInvalidParamException();
		return null;
	}
	
	public static Integer fetchImpodInt(String str){
		if(StringUtil.isEmpty(str) || emptySignore.equals(str)){
			return null;
		}
		else{
			return  Integer.valueOf(str);
		}
	}
	public static Long fetchImpodLong(String str){
		if(StringUtil.isEmpty(str) || emptySignore.equals(str)){
			return null;
		}
		else{
			return  Long.valueOf(str);
		}
	}
	
	public static String fetchImpodString(String str){
		if(StringUtil.isEmpty(str) || emptySignore.equals(str)){
			return null;
		}
		else{
			return str;
		}
	}
	
	
	public static String createIdNumberImplodStr(List<IdNumberVo> list,String spliter){
		if(list.size()==0){
			return emptySignore;
		}
		String[] vals = new String[list.size()];
		for(int i=0;i<list.size();i++){
			StringBuffer sb = new StringBuffer();
			sb.append(list.get(i).getId());
			sb.append("|");		
			sb.append(list.get(i).getNum());
			vals[i] =sb.toString();
		}
		return DBFieldUtil.implod(vals, spliter);
		
	}

	public static String implod(String[] items, String outterSplit) {
		if(items.length<=0){
			return null;
		}
	    StringBuffer out = new StringBuffer();
	    for(int i=0; i<items.length; i++) {
	        if(i!=0) {
	        	out.append(outterSplit);
	        }
	        if(items[i]==null){
	        	 out.append(emptySignore);
	        }
	        else{
		        out.append(items[i]);
	        }
	    }
	    return out.toString();
	}
	
	public static String implod(List<Integer> items, String outterSplit) {
		if(items.size()<=0){
			return null;
		}
	    StringBuffer out = new StringBuffer();
	    for(int i=0; i<items.size(); i++) {
	        if(i!=0) {
	        	out.append(outterSplit);
	        }
	        if(items.get(i)==null){
	        	 out.append(emptySignore);
	        }
	        else{
		        out.append(items.get(i));
	        }
	    }
	    return out.toString();
	}
	
	public static String implodLong(List<Long> items, String outterSplit) {
		if(items.size()<=0){
			return null;
		}
	    StringBuffer out = new StringBuffer();
	    for(int i=0; i<items.size(); i++) {
	        if(i!=0) {
	        	out.append(outterSplit);
	        }
	        if(items.get(i)==null){
	        	 out.append(emptySignore);
	        }
	        else{
		        out.append(items.get(i));
	        }
	    }
	    return out.toString();
	}
}
